<?php 
/**
 * php-script -  Job loader
 * 
 * @author $Author$
 * @version $Revision$
 */

/** Dependencies **/
include_file('src/10_conf/global_constants.php');
include_file('src/01_interfaces/Jobs-interfaces.inc.php');
include_file('src/02_class/JobsManager.class.php');
include_file('src/03_libs/Tools.class.php');
include_file('src/04_exceptions/JobManagerExceptions.class.php');

/**
 * php_jobs_launcher Class - Main Class to manage jobs
 * 
 * @author polux
 * @final
 */
final class php_jobs_launcher {
	
	/**
	 * Array of Jobs running
	 * 
	 * @var array(AJob)
	 */
	private static $_ao_jobsInProgress = array();
	
	
	/**
	 * Load and run a job immediately
	 * 
	 * @param 	string 		$p_s_jobName 	Job name to run 
	 * @return boolean
	 * @static
	 */
	static function runJob($p_s_jobName)
	{
		JobsManager::getLogManager()->addLogTitle("Running Job called '".$p_s_jobName."' ...");
		if(php_jobs_launcher::isJobPropertiesFileExists($p_s_jobName))
		{
			JobsManager::loadAndRunJob($p_s_jobName);
		}
 		else 
 			throw new JobNotFoundException($p_s_jobName);
	}
	
	/**
	 * Return absolute filepath for a job form his jobname
	 * 
	 * @param string $p_s_jobName
	 * @static
	 * @return string
	 * @uses JOB_INTERNAL_DIR_JOBFILE
	 * @uses JOB_INTERNAL_EXT_JOBFILE
	 */
	static function getDefaultFilePathForAJob($p_s_jobName)
	{
		return JOB_INTERNAL_DIR_JOBFILE.$p_s_jobName.JOB_INTERNAL_EXT_JOBFILE;
	}
	
	/**
	 * Return true if Properties file for a job exists
	 * 
	 * @param string $p_s_jobName
	 * @return boolean
	 */
	static function isJobPropertiesFileExists($p_s_jobName)
	{
		return file_exists(php_jobs_launcher::getDefaultFilePathForAJob($p_s_jobName));
	}	
	
	/**
	 * Return type of a Job
	 * 
	 * @param string $p_s_jobName
	 * @return string
	 */
	static function identifyTypeOfJobFromJobName($p_s_jobName)
	{
		return AJob::getTypeFromPropertiesFile(php_jobs_launcher::getDefaultFilePathForAJob($p_s_jobName), $p_s_jobName);
	}
	/**
	 * prepareDir - Check existance and create dir according parameters values
	 * 
	 * @param string 	$p_s_dirpath
	 * @param boolean 	$p_b_createIfNotExists
	 */
	static function prepareDir($p_s_dirpath,$p_b_createIfNotExists=false)
	{
		if(!file_exists($p_s_dirpath))
		{
			if($p_b_createIfNotExists)
			{
				mkdir($p_s_dirpath);
			}
		}
	}
	/**
	 * cleanDir - Delete all files in a directory
	 *
	 * @param string 	$p_s_dirpath	
	 */
	static function cleanDir($p_s_dirpath)
	{
		if(file_exists($p_s_dirpath))
		{
			rmdir($p_s_dirpath);
			mkdir($p_s_dirpath);
		}
	}
	/**
	 * checkFileExistance - Check if file exists in directory specified
	 * 
	 * @param string 	$p_s_dirpath	Path concerned
	 * @param string 	$p_s_filename	Filename to test
	 * @return bool
	 */
	static function checkFileExistance($p_s_dirpath,$p_s_filename)
	{
		$l_s_path = "";
		$l_s_filename = "";
		
		$l_s_path = $p_s_dirpath;
		if(strrpos($p_s_dirpath,APP_DIR_SEPERATOR)<> count($p_s_dirpath))
			$l_s_path .= APP_DIR_SEPERATOR;		
		
		if($p_s_filename[0] <> APP_DIR_SEPERATOR)
		{
			$l_s_filename = $p_s_filename;
		}
		else 
		{
			$l_s_filename = substr($p_s_filename,2);
		}
		
		return file_exists($l_s_path.$l_s_filename);
	}
}

?>